package com.ruoyi.web.task;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fs.domain.*;
import com.ruoyi.fs.service.*;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author hero
 */
@Component("fsTask")
public class DaliyTask {

    @Autowired
    private IFsAddressService fsAddressService;
    @Autowired
    private IFsCourtService fsCourtService;
    @Autowired
    private IFsLawyerService fsLawyerService;
    @Autowired
    private IFsCaseService fsCaseService;
    @Autowired
    private IFsClientService fsClientService;
    @Autowired
    private IFsCaseExamineService fsCaseExamineService;
    @Autowired
    private IFsCapitalService fsCapitalService;
    @Autowired
    private IFsTaskService fsTaskService;
    @Autowired
    private IFsTodoListService fsTodoListService;
    @Autowired
    private IFsFullViewService fsFullViewService;
    @Autowired
    private IFsBillService fsBillService;
    @Autowired
    private IFsRepayService fsRepayService;
    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysUserService sysUserService;

    private static Long ONE_DAY_TIME = 24 * 60 * 60 * 1000L;
    private static Long TWO_DAY_TIME = 2 * 24 * 60 * 60 * 1000L;
    private static Long THREE_DAY_TIME = 3 * 24 * 60 * 60 * 1000L;
    private static Long SIX_DAY_TIME = 6 * 24 * 60 * 60 * 1000L;
    private static Long ONE_WEEK_TIME = 7 * 24 * 60 * 60 * 1000L;
    private static Long TWO_WEEK_TIME = 14 * 24 * 60 * 60 * 1000L;
    private static Long HALF_MONTH = 15 * 24 * 60 * 60 * 1000L;

    private static DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 待办事项生成任务
     */
    public void dealDbsx() {
        FsTodoList query = new FsTodoList();
        query.setSfhl(0);
        List<FsTodoList> todoLists = fsTodoListService.selectFsTodoListList(query);
        if (!todoLists.isEmpty()) {
            Long now = System.currentTimeMillis();
            for (int i = 0; i < todoLists.size(); i++) {
                try {
                    FsTodoList todoList = todoLists.get(i);
                    String ah = todoList.getAh();
                    int lx = todoList.getDbsxlx();
                    String pd = null;
                    String content = "";
                    if (lx == 2) {
                        pd = todoList.getJzsj();
                        content = "距离交费截止时间还有";
                    } else if (lx == 4) {
                        pd = todoList.getKtsj();
                        content = "距离开庭时间还有";
                    }
                    Date pdd = sdf.parse(pd);

                    Long num = pdd.getTime() - now;
                    int type = 4;
                    int jb = 1;
                    if (num < ONE_DAY_TIME) {
                        content += "1天";
                        jb = 3;
                    } else if (num < THREE_DAY_TIME && num > TWO_DAY_TIME) {
                        content += "3天";
                        jb = 2;
                    } else if (num < ONE_WEEK_TIME && num > SIX_DAY_TIME) {
                        content += "1周";
                        jb = 1;
                    } else {
                        continue;
                    }
                    if (lx == 2) {
                        content += "，请及时交费。";
                    } else if (lx == 4) {
                        content += "，请提前准备。";
                        type = 5;
                    }
                    FsTask task = new FsTask();
                    if (StringUtils.isNotBlank(ah)) {
                        FsCase c = fsCaseService.selectFsCaseByAh(ah);
                        task.setAjzt(c.getZt());
                        task.setCaseId(c.getId());
                        task.setLsId(c.getOwnerId());
                    }
                    task.setJb(jb);
                    task.setJlrq(new Date());
                    task.setRwzt(1);
                    task.setType(type);
                    task.setContent(content);
                    fsTaskService.insertFsTask(task);
                } catch (Exception e) {

                }
            }
        }
    }


    /**
     * 待办事项生成任务
     */
    public void dealLaaj() {
        FsCase query = new FsCase();
        query.setAjzt(0);
        List<FsCase> list = fsCaseService.selectFsCaseList(query);
        if (!list.isEmpty()) {
            Long now = System.currentTimeMillis();
            for (int i = 0; i < list.size(); i++) {
                try {
                    FsCase fsCase = list.get(i);
                    String gxsj = fsCase.getGxsj();
                    Date gxTime;
                    if (gxsj.length() > 11) {
                        gxTime = df.parse(gxsj);
                    } else {
                        gxTime = sdf.parse(gxsj);
                    }
                    String content = "";

                    Long num = gxTime.getTime() - now;
                    int jb = 1;
                    int zt = fsCase.getZt();
                    int day = (int) ((double) num / (double) ONE_DAY_TIME);
                    if (num > THREE_DAY_TIME) {
                        jb = 1;
                    } else if (num > ONE_WEEK_TIME) {
                        jb = 2;
                    } else if (num > HALF_MONTH) {
                        jb = 3;
                    } else {
                        continue;
                    }
                    FsCaseExamine query2 = new FsCaseExamine();
                    query2.setCaseBh(fsCase.getBh());
                    List<FsCaseExamine> examines = fsCaseExamineService.selectFsCaseExamineList(query2);
                    String shyj = "";
                    if (!examines.isEmpty()) {
                        FsCaseExamine last = examines.get(examines.size() - 1);
                        shyj = last.getShyj();
                    }
                    int type = 6;
                    if (zt == 1) {
                        content = "案件审查中，已审查" + day + "天,请及时跟进！";
                    } else if (zt == 2) {
                        content = "案件已审查通过，已通过" + day + "天,请联系法院及时立案！</br>" + shyj;
                    } else if (zt == 3) {
                        content = "案件审查不通过，已过" + day + "天,请参考审核意见进行处理！</br>" + shyj;
                        type = 3;
                    } else if (zt == 4) {
                        content = "案件已立案，已通过" + day + "天,请联系法院及时开庭！</br>" + shyj;
                    } else if (zt == 5) {
                        content = "案件立案不通过，已过" + day + "天,请参考审核意见进行处理！</br>" + shyj;
                        type = 1;
                    } else if (zt == 6) {
                        content = "案件登记审核中，请及时跟进！";
                    } else if (zt == 7) {
                        content = "案件已登记，业务庭室审核中，请及时跟进！";
                    } else if (zt == 8) {
                        content = "案件已审核，请及时跟进！";
                    } else if (zt == 31) {
                        content = "案件材料待补正，请及时跟进！</br>" + shyj;
                        type = 2;
                    } else {
                        continue;
                    }
                    FsTask task = new FsTask();
                    task.setAjzt(fsCase.getZt());
                    task.setCaseId(fsCase.getId());
                    task.setLsId(fsCase.getOwnerId());
                    task.setJb(jb);
                    task.setJlrq(new Date());
                    task.setRwzt(1);
                    task.setType(type);
                    task.setContent(content);
                    fsTaskService.insertFsTask(task);
                } catch (Exception e) {

                }
            }
        }
    }


    /**
     * 还款生成任务
     */
    public void dealHk() {
        FsBill query = new FsBill();
        query.setSfkq("Y");
        query.setSfjq("N");
        List<FsBill> list = fsBillService.selectFsBillList(query);
        if (!list.isEmpty()) {
            Date now = new Date();
            Long nowTime = System.currentTimeMillis();
            for (int i = 0; i < list.size(); i++) {
                try {
                    FsBill bill = list.get(i);
                    FsRepay query2 = new FsRepay();
                    query2.setBillId(bill.getId());
                    List<FsRepay> repays = fsRepayService.selectFsRepayList(query2);
                    Date lastRepayDay = null;
                    if (!repays.isEmpty()) {
                        lastRepayDay = repays.get(0).getHkrq();
                    }
                    int hkfs = bill.getHkfs();
                    Date nextHkDay = now;
                    LocalDate csrq = LocalDate.parse(sdf.format(bill.getCsrq()), DateTimeFormatter.ISO_DATE);
                    if (hkfs == 1) {
                        nextHkDay = bill.getCsrq();
                    } else if (hkfs == 2) {
                        for (int j = 0; j < Integer.MAX_VALUE; j++) {
                            LocalDate next = csrq.plusMonths(j);
                            Date nTime = sdf.parse(next.format(DateTimeFormatter.ISO_DATE));
                            if (nTime.after(now)) {
                                nextHkDay = nTime;
                                break;
                            }
                        }
                    } else if (hkfs == 3) {
                        for (int j = 0; j < Integer.MAX_VALUE; j++) {
                            LocalDate next = csrq.plusMonths(j * 3);
                            Date nTime = sdf.parse(next.format(DateTimeFormatter.ISO_DATE));
                            if (nTime.after(now)) {
                                nextHkDay = nTime;
                                break;
                            }
                        }
                    } else if (hkfs == 4) {
                        for (int j = 0; j < Integer.MAX_VALUE; j++) {
                            LocalDate next = csrq.plusMonths(j * 12);
                            Date nTime = sdf.parse(next.format(DateTimeFormatter.ISO_DATE));
                            if (nTime.after(now)) {
                                nextHkDay = nTime;
                                break;
                            }
                        }
                    } else {
                        continue;
                    }
                    if (lastRepayDay != null && (nextHkDay.getTime() - lastRepayDay.getTime()) < ONE_WEEK_TIME) {
                        continue;
                    }
                    FsCase c = fsCaseService.selectFsCaseById(bill.getCaseId());

                    Long num = nextHkDay.getTime() - nowTime;
                    int type = 7;
                    int jb = 1;
                    if (num < ONE_DAY_TIME) {
                        jb = 3;
                    } else if (num < THREE_DAY_TIME && num > TWO_DAY_TIME) {
                        jb = 2;
                    } else if (num < ONE_WEEK_TIME && num > SIX_DAY_TIME) {
                        jb = 1;
                    } else {
                        continue;
                    }
                    FsClient bg = fsClientService.selectFsClientById(c.getBg());
                    String content = "还款人：" + bg.getXm() + "，联系电话：" + bg.getSjhm() + "，下一个还款日：" + sdf.format(nextHkDay);
                    FsTask task = new FsTask();
                    task.setAjzt(c.getZt());
                    task.setCaseId(c.getId());
                    task.setLsId(c.getOwnerId());
                    task.setJb(jb);
                    task.setJlrq(new Date());
                    task.setRwzt(1);
                    task.setType(type);
                    task.setContent(content);
                    fsTaskService.insertFsTask(task);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
